package com.own.core.html.table;

import org.apache.wicket.Component;
import org.apache.wicket.ajax.markup.html.navigation.paging.AjaxPagingNavigationLink;
import org.apache.wicket.ajax.markup.html.navigation.paging.AjaxPagingNavigator;
import org.apache.wicket.behavior.AbstractBehavior;
import org.apache.wicket.markup.ComponentTag;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.navigation.paging.IPageable;
import org.apache.wicket.markup.html.navigation.paging.IPagingLabelProvider;
import org.apache.wicket.markup.html.navigation.paging.PagingNavigator;

public class OwnAjaxPagingNavigator extends AjaxPagingNavigator {

	private final IPagingLabelProvider labelProvider;
	public OwnAjaxPagingNavigator(String id, IPageable pageable,
			IPagingLabelProvider labelProvider) {
		super(id, pageable, labelProvider);
		this.labelProvider=labelProvider;
	}

	public OwnAjaxPagingNavigator(final String id, final IPageable pageable) {
		this(id, pageable, null);
	}

	
	@Override
	protected void onBeforeRender()
	{
		if (get("first") == null)
		{
			// Get the navigation bar and add it to the hierarchy
//			pagingNavigation = newNavigation(pageable, labelProvider);
//			add(newNavigation(getPageable(), labelProvider));

			// Add additional page links
			add(newPagingNavigationLink("first", getPageable(), 0).add(
				new TitleAppender("PagingNavigator.first")));
			add(newPagingNavigationIncrementLink("prev", getPageable(), -1).add(
				new TitleAppender("PagingNavigator.previous")));
			add(newPagingNavigationIncrementLink("next", getPageable(), 1).add(
				new TitleAppender("PagingNavigator.next")));
			add(newPagingNavigationLink("last", getPageable(), -1).add(
				new TitleAppender("PagingNavigator.last")));
			add(newOwnPagingNavigationLink("all", getPageable(), -1).add(
					new TitleAppender("PagingNavigator.last")));
		}
		super.onBeforeRender();
	}
	
	
//	@Override
	protected Link<?> newOwnPagingNavigationLink(String id, IPageable pageable, int pageNumber)
	{
		return new OwnAjaxPagingNavigationLink(id, pageable, pageNumber);
	}

	
	
	
	private final class TitleAppender extends AbstractBehavior
	{
		private static final long serialVersionUID = 1L;

		private final String resourceKey;

		/**
		 * Constructor
		 * 
		 * @param resourceKey
		 *            resource key of the message
		 */
		public TitleAppender(String resourceKey)
		{
			this.resourceKey = resourceKey;
		}

		/** {@inheritDoc} */
		@Override
		public void onComponentTag(Component component, ComponentTag tag)
		{
			tag.put("title", OwnAjaxPagingNavigator.this.getString(resourceKey));
		}
	}
}
